package com.cookor.poi.modules.mx.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.mx.entity.TMxOrder;
import com.cookor.poi.modules.mx.service.TMxOrderService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/mx/tMxOrder"})
public class TMxOrderController extends BaseController {
  @Autowired
  private TMxOrderService tMxOrderService;
  
  @ModelAttribute
  public TMxOrder get(@RequestParam(required = false) String id) {
    TMxOrder entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tMxOrderService.get(id); 
    if (entity == null)
      entity = new TMxOrder(); 
    return entity;
  }
  
  @RequiresPermissions({"mx:tMxOrder:view"})
  @RequestMapping({"list", ""})
  public String list(TMxOrder tMxOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
    Page<TMxOrder> page = this.tMxOrderService.findPage(new Page(request, response), tMxOrder);
    model.addAttribute("page", page);
    return "modules/mx/tMxOrderList";
  }
  
  @RequiresPermissions({"mx:tMxOrder:view"})
  @RequestMapping({"form"})
  public String form(TMxOrder tMxOrder, Model model) {
    model.addAttribute("tMxOrder", tMxOrder);
    return "modules/mx/tMxOrderForm";
  }
  
  @RequiresPermissions({"mx:tMxOrder:edit"})
  @RequestMapping({"save"})
  public String save(TMxOrder tMxOrder, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tMxOrder, new Class[0]))
      return form(tMxOrder, model); 
    this.tMxOrderService.save(tMxOrder);
    addMessage(redirectAttributes, new String[] { "保存镁信订单成功" });
    return "redirect:" + Global.getAdminPath() + "/mx/tMxOrder/?repage";
  }
  
  @RequiresPermissions({"mx:tMxOrder:edit"})
  @RequestMapping({"delete"})
  public String delete(TMxOrder tMxOrder, RedirectAttributes redirectAttributes) {
    this.tMxOrderService.delete(tMxOrder);
    addMessage(redirectAttributes, new String[] { "删除镁信订单成功" });
    return "redirect:" + Global.getAdminPath() + "/mx/tMxOrder/?repage";
  }
}
